﻿using System.Windows;
using System.Windows.Controls;
using ManagedChangeJournal;
using ManagedChangeJournal.Native.Journal;

namespace ChangeJournalBrowser
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            //ChangeJournal = new ChangeJournal();
        }

        #region Dependency Properties

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty ChangeJournalProperty =
            DependencyProperty.Register("ChangeJournal", typeof(ChangeJournal),
            typeof(Window), new PropertyMetadata(new ChangeJournal()));

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public ChangeJournal ChangeJournal
        {
            get { return (ChangeJournal)GetValue(ChangeJournalProperty); }
            set { SetValue(ChangeJournalProperty, value); }
        }

        #endregion

        #region Event Handlers

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PopulateVolumeSelector();
        }


        private void lviewVolumes_SelectionChanged(object sender,
            SelectionChangedEventArgs e)
        {
            CloseJournal();

            if (lviewVolumes.SelectedItem != null)
            {
                OpenJournal(lviewVolumes.SelectedItem.ToString());
            }
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            CloseJournal();

            if (lviewVolumes.SelectedItem != null)
            {
                OpenJournal(lviewVolumes.SelectedItem.ToString());
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            CloseJournal();
        }

        private void btnActivate_Click(object sender, RoutedEventArgs e)
        {
            ActivateJournal();
        }

        private void btnDeactivate_Click(object sender, RoutedEventArgs e)
        {
            DeactivateJournal();
        }
        
        private void btnNextSet_Click(object sender, RoutedEventArgs e)
        {
            if (ChangeJournal.Data.IsActive)
            {
                lviewEntries.Items.Clear();
                ChangeJournal.Read(ChangeReason.All, false, 0);
                foreach (var entry in ChangeJournal.Entries)
                {                    
                    //string[] {
                    //    entry.Name, entry.Reason.ToString("F") };
                    lviewEntries.Items.Add(entry);
                }
            }
        }

        private void btnPreviousSet_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        private void PopulateVolumeSelector()
        {
            lviewVolumes.Items.Clear();
            foreach (var volume in VolumeHelper.GetValidVolumePaths())
            {
                lviewVolumes.Items.Add(volume);
            }
        }

        private void OpenJournal(string volume)
        {
            ChangeJournal.Open(volume);

            EnableActionsAccordingly();

            PopulateJournalData();
        }

        private void CloseJournal()
        {
            if (ChangeJournal.IsOpen)
                ChangeJournal.Close();

            EnableActionsAccordingly();

            PopulateJournalData();

            lviewEntries.Items.Clear();
        }

        private void ActivateJournal()
        {
            if (ChangeJournal.IsOpen)
            {
                if (!ChangeJournal.Data.IsActive)
                {
                    ChangeJournal.Create(0, 0);
                }
            }

            EnableActionsAccordingly();

            PopulateJournalData();
        }


        private void DeactivateJournal()
        {
            if (ChangeJournal.IsOpen)
            {
                if (ChangeJournal.Data.IsActive)
                {
                    ChangeJournal.Delete();
                }
            }

            EnableActionsAccordingly();

            PopulateJournalData();

            lviewEntries.Items.Clear();
        }

        private void PopulateJournalData()
        {
            textJournalID.Text = ChangeJournal.Data.ID.ToString();
            textLowestValidUSN.Text = ChangeJournal.Data.LowestValidUSN.ToString();
            textMaximumUSN.Text = ChangeJournal.Data.MaximumUSN.ToString();
            textMaximumSize.Text = ChangeJournal.Data.MaximumSize.ToString() + " bytes";
            textAllocationDelta.Text = ChangeJournal.Data.AllocationDelta.ToString() + " bytes";
            textStatus.Text = !ChangeJournal.IsOpen ? "Journal volume not open." : "Journal volume open" + 
                (ChangeJournal.Data.IsActive ? " and active." : " but not active.");
        }

        private void EnableActionsAccordingly()
        {
            btnOpen.IsEnabled = !ChangeJournal.IsOpen;
            btnClose.IsEnabled = ChangeJournal.IsOpen;
            btnActivate.IsEnabled = !ChangeJournal.Data.IsActive;
            btnDeactivate.IsEnabled = ChangeJournal.Data.IsActive;
            btnNextSet.IsEnabled = ChangeJournal.Data.IsActive;
            //btnPreviousSet.IsEnabled = ChangeJournal.Data.IsActive;
        }

    }
}
